/* grid.h is part of VIP program
 * Written by J. Salvador Arias, 2010
 *      CONICET, INSUE, Instituto Miguel Lillo,
 *      Miguel Lillo 205, S.M. de Tucuman (4000)
 *      Tucuman, Argentina
 *      e-mail: jsalarias@csnat.unt.edu.ar
 *
 * VIP is a free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation and
 * Creative Commons.
 *
 * This file and the VIP program is distributed in the hope
 * that it will be useful, but WITHOUT ANY WARRANTY; without
 * even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * To view a copy of this license, visit
 * http://creativecommons.org/licenses/GPL/2.0/
 * or send a letter to
 * Creative Commons, 171 Second Street, Suite 300,
 * San Francisco, California, 94105, USA.
 */


#ifndef GRID_HEADER_INCLUDED
#define GRID_HEADER_INCLUDED

#include "ghead.h"
#include "bitfield.h"
#include "biogeo.h"

typedef struct tagQUADTREE QUADTREE;
struct tagQUADTREE {
 SINTEGER firstRow;
 SINTEGER lastRow;
 SINTEGER firstCol;
 SINTEGER lastCol;
 SCOUNTER cell;
 SINTEGER size;
 SFLAG state;
 SFLAG obsCell;
 GNode* node;
};

/* ------ Global variables ------ */
SCOUNTER numCells;
SCOUNTER numRows;
SCOUNTER numCols;

#define GETNUMCELLS numCells
#define GETNUMCOLS  numCols
#define GETNUMROWS  numRows

SREAL actualGridSize;
SREAL gridStartX;
SREAL gridStartY;
SCOUNTER actualFill;

SFLAG gridMode;

#define GETGRIDSIZE   actualGridSize
#define GETGRIDSTARTX gridStartX
#define GETGRIDSTARTY gridStartY
#define GETFILL       actualFill

#define SETGRIDSIZE(x)   actualGridSize = (x)
#define SETFILL(x)       actualFill = (x)

#define MOORE_NEIGHBORS      0x01
#define VONNEUMANN_NEIGHBORS 0x02

#define GETGRIDMODE     gridMode
#define SETGRIDMODE(x)  gridMode = (x)

/* ------ Functions ------ */
SCOUNTER GetCell (SINTEGER row, SINTEGER col);
void CreateGrid ();
void DestroyGrid ();

SFLAG GetGridState ();

void MapIntoQTree (BITFIELD* setA, BITFIELD* setB);
SFLAG GetRowColState (SINTEGER row, SINTEGER col);
void GetColRowFromCell (SCOUNTER cell, SINTEGER* row, SINTEGER* col);

SCOUNTER GetCellIfObs (SINTEGER row, SINTEGER col);

void DoFill (BITFIELD* dest, BITFIELD* obs, SCOUNTER fillValue);
void GetObsSet (BITFIELD* obs, GNode* refNode);

SCOUNTER CalculatePhyloDiv (SCOUNTER cell, SCOUNTER max);

#endif
